﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Media;

namespace GuiMongo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            string directory = Directory.GetCurrentDirectory();
            string databfolder = String.Format(@"{0}/data", directory); /* data folder path */
            string dbfolder = String.Format(@"{0}/db", databfolder); /* db folder path   */
            try
            {
                /* data and db folder */
                if (!Directory.Exists(databfolder))
                {
                    Directory.CreateDirectory(databfolder);
                }
                if (!Directory.Exists(dbfolder))
                {
                    Directory.CreateDirectory(dbfolder);
                }
                /* data and db folder end */
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            string directory = Directory.GetCurrentDirectory();
            string shellOpen = string.Format(@"{0}\bin\mongod.exe", directory);
            var p = new Process
            {
                StartInfo =
                {
                    FileName = shellOpen,
                    Arguments = string.Format(@"--dbpath {0}\data\db --port 27017", directory)
                }
            };
            bool start = p.Start();
         
            if (start)
            {
                var mySolidColorBrush = new SolidColorBrush();
                mySolidColorBrush.Color = Color.FromArgb(255, 0, 255, 0);
                EStart.Fill = mySolidColorBrush;
                BtnStart.IsEnabled = false;
                BtnStop.IsEnabled = true;
              
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (process.ProcessName == "mongod")
                {
                    process.Kill();
                }
            }
            var mySolidColorBrush = new SolidColorBrush();
            mySolidColorBrush.Color = Color.FromArgb(255, 255, 0, 0);
            EStart.Fill = mySolidColorBrush;
            BtnStart.IsEnabled = true;
            BtnStop.IsEnabled = false;
            var path = Environment.CurrentDirectory + "\\data\\db\\mongod.lock";
            Process.Start("cmd.exe", string.Format("/c del \"{0}", path));
        }

        private void btncmd_Click(object sender, RoutedEventArgs e)
        {
            Process p = new Process
            {
                StartInfo =
                {
                    FileName = "cmd.exe",
                    UseShellExecute = true
                }
            };
            p.Start();
        }

        private void btnexplorer_Click(object sender, RoutedEventArgs e)
        {
            string directory = Directory.GetCurrentDirectory();
            Process.Start(string.Format("{0}",directory));
        }

        private void ExitApp(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (process.ProcessName == "mongod")
                {
                    process.Kill();
                }
            }
        }
    }
}
